package com.quanyan.stadium.service.gameRules;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.quanyan.stadium.entity.TbGameEvent;
import com.quanyan.stadium.entity.TbTeamDoubleAddendum;
import com.quanyan.stadium.transfer.GameConfigTransfer;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoxiao on 16/11/11.
 *
 * 玩法抽象类
 */
public abstract class AbsGameCategory {

    @Autowired
    private GameConfigTransfer gameEventTransfer;


    /**
     * 根据玩法,获取比赛队伍单位数目
     *
     * 备注:
     *      1 双打认为两个人为一个队伍单位
     *      2 大球类认为一个俱乐部为一个队伍单位
     *      3 单打认为一个人为一个队伍单位
     * */
    public int getTeamNum(TbGameEvent tbGameEvent, Integer currentTeamNum){
        if (gameEventTransfer.getGameCategoryById(tbGameEvent.getGameCategoryType().intValue()).getMinNum() == 2
                && gameEventTransfer.getGameCategoryById(tbGameEvent.getGameCategoryType().intValue()).getMaxNum() == 2) {
            currentTeamNum = currentTeamNum/2;
        }
        return currentTeamNum;
    };


    /**
     * 多人队伍转换
     *
     *
     * */
    public List<Integer> transferTeamIdToMultiTeam(List<TbTeamDoubleAddendum> addendums, List<Integer> teamIds){
        if (null == addendums || addendums.size() < 1) {
            return teamIds;
        }

        int maxGroupNum = teamIds.size()/2;
        Gson gson = new Gson();
        List<Integer> newTeams = new ArrayList<>();
        while (newTeams.size() < maxGroupNum) {
            for (TbTeamDoubleAddendum tbTeamDoubleAddendum : addendums) {
                List<Integer> tempList =gson.fromJson(tbTeamDoubleAddendum.getTeamIdGson() ,new TypeToken<List<Integer>>() {
                }.getType());
                if (teamIds.size()>0 && tempList.contains(teamIds.get(0))) {
                    teamIds.removeAll(tempList);
                    newTeams.add(tbTeamDoubleAddendum.getId());
                }
            }
        }
        return newTeams;
    }

    /**
     * 获取当前队伍id的列表
     *
     *
     * */
    public List<Integer> getTeamIdsByCategory(TbGameEvent tbGameEvent, Integer currentTeamNum){
        //对逻辑映射
        List<Integer> teamIds = new ArrayList<>();
        /*if (reqChallenge.getIsDouble()) {
            TbTeamDoubleAddendumExample example = new TbTeamDoubleAddendumExample();
            TbTeamDoubleAddendumExample.Criteria criteria = example.createCriteria();
            criteria.andGameEventIdEqualTo(reqChallenge.getGameEventId());
            criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
            List<TbTeamDoubleAddendum> list = tbTeamDoubleAddendumMapper.selectByExample(example);
            for (TbTeamDoubleAddendum tbTeamDoubleAddendum : list) { //队伍id集合
                teamIds.add(tbTeamDoubleAddendum.getId());
            }
            reqChallenge.setTbTeamDoubleAddendumList(list);
        }else {
            for (TbGameEventTeam tbGameEventTeam : tbGameEventTeamList) { //队伍id集合
                teamIds.add(tbGameEventTeam.getId());
            }
        }*/
        return teamIds;
    }


}
